﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using Website.Interfaces;
using Website.Models;

namespace Website.Controllers
{
    public class PaintingsController : Controller
    {
        private IArtCollection _artCollection;
        private IImageManager _imageManager;

        public PaintingsController(IArtCollection artcollection, IImageManager imageManager)
        {
            _artCollection = artcollection;
            _imageManager = imageManager;
        }

        [HttpGet]
        [Authorize]
        public ActionResult Manage()
        {
            var paintings = _artCollection.Paintings;
            return View(paintings);
        }

        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(Painting painting)
        {
            if (Request.Files.Count > 0)
            {
                var postedFile = Request.Files[0] as HttpPostedFileBase;
                string filename = _imageManager.SavePostedFile(postedFile);

                painting.Filename = filename;
                _imageManager.SaveThumbnail(filename);
                
                _artCollection.AddPainting(painting);
            }

            return RedirectToAction("Manage", "Paintings");
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            var painting = (from p in _artCollection.Paintings
                            where p.Id == id
                            select p).First();
            return View(painting);
        }

        [HttpPost]
        public ActionResult Edit(Painting painting)
        {
            if (ModelState.IsValid)
            {
                _artCollection.DeletePainting(painting.Id);
                _artCollection.AddPainting(painting);
            }
            return RedirectToAction("Manage", "Paintings");
        }

        [HttpGet]
        public ActionResult Delete(int id)
        {
            var painting = (from p in _artCollection.Paintings
                            where p.Id == id
                            select p).First();

            // delete image files
            _imageManager.DeleteImage(painting.Filename);
            _imageManager.DeleteThumbnail(painting.Filename);
            
            // delete record from collection
            _artCollection.DeletePainting(id);
            
            return View(painting);
        }

        [HttpPost]
        public ActionResult Delete(int id, Painting painting)
        {
            _artCollection.DeletePainting(id);
            return RedirectToAction("Manage", "Paintings");
        }

        public ActionResult MoveUp(int id)
        {
            _artCollection.PromotePainting(id);

            return RedirectToAction("Manage", "Paintings");
        }

        public ActionResult MoveDown(int id)
        {
            _artCollection.DemotePainting(id);

            return RedirectToAction("Manage", "Paintings");
        }
    }
}
